package test0ne;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class LocalDataUtil {
    public static void main(String[] args) {
        //获取当前日期
        LocalDate localDate = LocalDate.now();          //输出 2020-03-18
        System.out.println(localDate);

        //主动构造
        LocalDate localDate11 = LocalDate.of(2020, 03, 18);    //输出 2020-03-18
        System.out.println(localDate11);


        LocalDate localDate1 = LocalDate.of(2021, 01,01);
        LocalDate localDate2 = LocalDate.of(2020, 01,01);
        Period period = Period.between(localDate1, localDate2);
        System.out.println(period.getDays());
        System.out.println(period.getMonths());
        System.out.println(period.getYears());
        System.out.println(localDate2.toEpochDay() - localDate1.toEpochDay());

        //Date转LocalDate
        Date date = new Date();
        LocalDate dateToLocalDate = LocalDate.ofInstant(date.toInstant(), ZoneId.systemDefault());    //输出 2020-03-18
        System.out.println(dateToLocalDate);

        //获取当前时间
        LocalTime localTime = LocalTime.now();
        System.out.println("localTime:"+localTime);

        //当前日期时间
        LocalDateTime localDateTime1 = LocalDateTime.of(2020,4,6,23,0,0);
//        System.out.println("添加秒数"+localDateTime1.plusSeconds(180l).format(new DateTimeFormatter("yyyyMMdd HH:mm:ss")));

        LocalDateTime localDateTime2 = LocalDateTime.of(2020, 4, 7, 23, 3, 12);
//      LocalDateTime localDateTime1 = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());

        //计算间隔时间
            Duration duration = Duration.between(localDateTime1, localDateTime2);
        System.out.println("间隔天数："+duration.toDays());
        System.out.println("间隔小时数："+duration.toHours());
        System.out.println("间隔分钟数："+duration.toMinutes());
        System.out.println("间隔秒数："+duration.toSeconds());

        //        System.out.println("转化为时分秒："+ localDateTime.format(DateTimeFormatter.ofPattern("HH:mm:ss")));

        LocalDateTime expireDate = LocalDateTime.of(2021,4,13,12,0,0);
        LocalDateTime now = LocalDateTime.now();
        long duration1 = Duration.between(now, expireDate).toDays();
        System.out.println(duration1);
    }
}
